//
//  HMStatusPictureView.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/16.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMStatusPictureView.h"
#import "HMPictureViewCell.h"
#import <SDImageCache.h>
#define HMStatusPictureItemMaxCol 3
#define HMStatusPictureItemWH ([UIScreen mainScreen].bounds.size.width-2*HMStatusCellMargin-(HMStatusPictureItemMaxCol-1)*HMStatusPictureItemMargin)/HMStatusPictureItemMaxCol
#define HMStatusPictureItemMargin 4

static NSString *ReuseIdentifier = @"item";
@interface HMStatusPictureView ()<UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

@implementation HMStatusPictureView

-(instancetype)initWithFrame:(CGRect)frame
{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.layout.minimumInteritemSpacing = HMStatusPictureItemMargin;
    self.layout.minimumLineSpacing = HMStatusPictureItemMargin;
  
    self = [super initWithFrame:CGRectZero collectionViewLayout:self.layout];
    
    //    注册cell
    [self registerClass:[HMPictureViewCell class] forCellWithReuseIdentifier:ReuseIdentifier];
    
    self.dataSource = self;
    
    self.backgroundColor = [UIColor clearColor];
    
    return  self;
    
}

-(void)setPic_urlsArr:(NSArray *)pic_urlsArr
{
    _pic_urlsArr = pic_urlsArr;
    
    CGSize size = [self caculateSize];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
    }];
    
//    因为复用问题,需要重新加载
    [self reloadData];
}

-(CGSize )caculateSize
{
    NSInteger count = _pic_urlsArr.count;
    
    CGSize size = CGSizeMake(HMStatusPictureItemWH, HMStatusPictureItemWH);
    
    self.layout.itemSize = size;
    
    NSInteger col = HMStatusPictureItemMaxCol;
    
    if (count == 0) {
        return CGSizeZero;
    }
    
    if (count == 1) {
        
        NSURL *url = [_pic_urlsArr firstObject];
        UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url.absoluteString];
        
        size = image.size;
        size = CGSizeMake(size.width*[UIScreen mainScreen].scale, size.height*[UIScreen mainScreen].scale);
        
        if (size.width < 40)
        {
            size.width =  40;
        }
        CGFloat halfScreenWidth = [UIScreen mainScreen].bounds.size.width*0.5;
        if (size.width > halfScreenWidth) {
            size.width = halfScreenWidth;
        }
        
//        开发中一般都是定宽不定高
        self.layout.itemSize = size;
        
        return size;
    }
    
    if (count == 2 || count == 4)
    {
        col = 2;
    }
    
//    计算行数
    NSInteger row = (count + col - 1)/col;
    
//    计算宽度
    CGFloat width = col*HMStatusPictureItemWH + (col-1)*HMStatusPictureItemMargin;
    
//    计算高度
    CGFloat height = row *HMStatusPictureItemWH + (row -1)*HMStatusPictureItemMargin;
    
    return CGSizeMake(width, height);
}

#pragma mark - 数据源方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pic_urlsArr.count == 0 ? 0 : self.pic_urlsArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HMPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier forIndexPath:indexPath];
    
    cell.imageURL = self.pic_urlsArr[indexPath.item];
    
//    HMLog(@"%@",NSStringFromCGRect(cell.bounds));
    
    return cell;
}

@end













